import { translateByCode } from '@/lib/getTransData';
const validator = {
  callback(rule, message) {
    if (rule && message) {
      rule.funMessage = message;
      return false;
    }
    return true;
  },
  required(msg, trigger) {
    return {
      required: true,
      message: (value, rule) => {
        if (value === '' || !value || !value.length) {
          return msg;
        }
        return rule.funMessage;
      },
      // 函数验证信息
      funMessage: '',
      trigger: trigger || ''
    };
  },
  /* 验证身份证号码 */
  checkIdNumber: (value, rule) => {
    const idNumber = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/;
    if (value && value !== '' && !idNumber.test(value)) {
      validator.callback(rule, '身份证号码格式非法');
      return false;
    }
    validator.callback(rule);
    return true;
  },
  /* 手机号码验证 */
  checkPhone: (value, rule) => {
    if (value && value !== '' && !/^1[0123456789]\d{9}$/.test(value)) {
      validator.callback(rule, '手机号码格式错误');
      return false;
    }
    validator.callback(rule);
    return true;
  },
  /* 登录密码 密码由6-16位的数字、字母、特殊字符(!@#&*_)组成 */
  /* 登录密码 密码由6-12位的数字、字母组成 */
  checkPwd: (value, rule) => {
    // const validReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#&*_])[\da-zA-Z~!!@#&*_]{6,16}$/;
    const validReg = /^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,12}$/;
    if (value && value !== '' && !validReg.test(value)) {
      validator.callback(rule, translateByCode('rules.validPassword'));
      return false;
    }
    validator.callback(rule);
    return true;
  }
};
export default validator;
